package com.bookmanager.www.common.config;

/**
 * Created with IntelliJ IDEA.
 *
 * @author: 风离
 * @Date: 2021/09/02/0:52
 * @Description:
 */




import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import io.swagger.annotations.Api;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;

import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;

import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;


import java.util.ArrayList;
import java.util.List;

@Configuration
//开启swagger
@EnableSwagger2
//@EnableSwaggerBootstrapUI
public class Swagger2Config {


//    private  List<Parameter> headerInfo(){
//        ParameterBuilder ticketPar = new ParameterBuilder();
//        List<Parameter> pars = new ArrayList<Parameter>();
//        ticketPar.name("Authorization").description("access_token")//name表示名称，description表示描述
//                .name("access-control-expose-headers").description("Authorization")
//                .modelRef(new ModelRef("string")).parameterType("header")
//                .required(true).defaultValue("Bearer ").build();//required表示是否必填，defaultvalue表示默认值
//        pars.add(ticketPar.build());//添加完此处一定要把下边的带***的也加上否则不生效
//        return pars;
//    }

    //配置了Swagger的Docket的bean实例
    @Bean
    public Docket docket(){

        Docket docket = new Docket(DocumentationType.SWAGGER_2);
//        //设置swagger整合JWT的认证
//
//        //设置令牌在header中，名为token，描述为token
//        ApiKey apiKey = new ApiKey("token1", "authorization", "header");
//        ApiKey apiKey2 = new ApiKey("token2", "access-control-expose-headers", "header");
//        List<SecurityScheme> apiKeyList = new ArrayList<>();
//        apiKeyList.add(apiKey);
//        apiKeyList.add(apiKey2);
//        docket.securitySchemes(apiKeyList);
//
//        //设置令牌起作用的范围为全局
//        AuthorizationScope scope = new AuthorizationScope("global", "accessEverything");
//        AuthorizationScope[] scopes = {scope};
//        SecurityReference reference = new SecurityReference("token", scopes);
//        List refList = new ArrayList();
//        refList.add(reference);
//
//        //需要封装到securityContexts才能被docket添加
//        SecurityContext context = SecurityContext.builder().securityReferences(refList).build();
//        List cxtList = new ArrayList();
//        cxtList.add(context);
//        docket.securityContexts(cxtList);
        docket.select()//获取Docket中的选择器,返回apiSelectorBuilder构造选择器 如扫描什么包的注解
                //定位到开启@ApiOperation 注解的方法上
//                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
//                .apis(RequestHandlerSelectors.basePackage("top.flya.index.controller"))//设定扫描那个包（包含子包）
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .paths(PathSelectors.any())
                .build().apiInfo(apiInfo2());

        return docket;
    }

//    private  ApiInfo apiInfo(){
//        Contact contact=new Contact("dyk","https://blog.csdn.net/qq_44866153","1106649325@qq.com");
//        return new ApiInfo(
//                "dyk的swagger文档",
//                "我的 API文档",
//                "v1.0",
//                "https://blog.csdn.net/qq_44866153",
//                contact,
//                "Apache 2.0",
//                "http://www.apache.org/licenses/LICENSE-2.0",
//                new ArrayList<>()
//
//
//        );
//    }

    private ApiInfo apiInfo2(){

        return new ApiInfoBuilder()
                .title("网上书店的swagger文档")
                .description("我的 API文档")
                .version("v1.0")
                .contact(new Contact("风离------苏才华","https://flya.top/",""))
//                .license("Apache 2.0")
//                .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0")
                .build();
    }
}

